# Copyright (C) 2009, Mathias Gyllengahm
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.


import threading
import time
from cPickle import Pickler, Unpickler

class LogfileHandler(threading.Thread):
    def __init__(self, eventfilename):
        threading.Thread.__init__(self)
        self.eventfilename = eventfilename
        self.eventfile = None
        self.pickler = None
        self.callback = None
        self.ignoreTime = False
    #------------------------------------------------------------
    def startlogging(self):
        self.eventfile = open(self.eventfilename, "wb")
        self.pickler = Pickler(self.eventfile, 2)
    #------------------------------------------------------------
    def event(self, type, event):
        #print "Logging " + str(type) + ":" + str(event)
        entry = [ time.time(), type, event]
        self.pickler.dump(entry) 
        self.eventfile.flush()
    #------------------------------------------------------------
    def stoplopping(self):
        self.eventfile.close()
    #------------------------------------------------------------
    def run(self):
        self.eventfile = open(self.eventfilename, "rb")
        unpickler = Unpickler(self.eventfile)
        curstarttime = time.time()

        # Read first event, get start time for replay file
        entry = unpickler.load()
        filestarttime = entry[0]
        self.callback(entry[1], entry[2])

        while True:
            try:
                entry = unpickler.load()
                eventtime = entry[0]
                type = entry[1]
                event = entry[2]
                #print str(type) + ":" + str(event)
                timeoffset = time.time() - curstarttime
                filetimeoffset = eventtime - filestarttime
                #print "toffset=" + str(timeoffset) + " ftimeoffset=" + str(filetimeoffset)
                if (filetimeoffset - timeoffset) > 0.0 and self.ignoreTime == False:
                    #print "Sleeping " + str(filetimeoffset - timeoffset)
                    time.sleep(filetimeoffset - timeoffset)
                self.callback(type, event)
            except EOFError:
                break
                print "Augh"
    #------------------------------------------------------------
    def replayfile(self, callback, ignoreTime = False):
        self.callback = callback
        self.start() # Start thread
        self.ignoreTime = ignoreTime

